// pages/object/put.js
const Auth = require('../../resources/bce-bos-uploader-lite/bce-sdk-js/auth.js');
const Q = require('../../resources/bce-bos-uploader-lite/vendor/q.js');
const BosClient = require('../../resources/bce-bos-uploader-lite/bce-sdk-js/bos_client.js');
const PutObjectTask = require('../../resources/bce-bos-uploader-lite/put_object_task.js');
const MultipartTask = require('../../resources/bce-bos-uploader-lite/multipart_task.js');
const fs = wx.getFileSystemManager();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindPutObjectTask : function(){
	let that = this;
    wx.chooseImage({
      success: function (res) {
        fs.readFile({
          filePath: res.tempFilePaths[0],
          success: function (buffer) {
            var data = buffer.data;
            that.initPutObjectTask(data);
          }
        });
      },
    });
  },
  
  initPutObjectTask:function(data){
	  var that = this;
	  wx.request({
		  url:'http://172.17.150.42:8080/web/api/bos/ack.do',
		  dataType:'json',
		  method:'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        sts:'{}'
      },
		  success:function(res){
			   var key = "test/res/image/" + new Date().getTime() + '.jpg';
			   let ak = res.data.result.AccessKeyId,sk = res.data.result.SecretAccessKey,sessionToken = res.data.result.SessionToken
			   let client = new BosClient({
				 endpoint : 'http://bj.bcebos.com',
				 credentials : {
					 ak: ak ,
					 sk: sk
				 },
				 sessionToken : sessionToken,
				  createSignature : function (_, httpMethod, path, params, headers) {
					var credentials = {
					  ak: ak,
					  sk: sk
					};
					return Q.fcall(function () {
					  var auth = new Auth(credentials.ak, credentials.sk);
					  return auth.generateAuthorization(httpMethod, path, params, headers);
					});
				  }
			  });
			  var task = new PutObjectTask(client,that,{
                bucket: 'zskd-pub',
                object: key,
                file : {
					blob : data,
					name : key
				}
              }
            );
			  task.start(3);
		  }	
	  });
  },

  dispatchEvent : function(eventName, eventArguments, throwErrors){
	  
  },
  
  /**
   * 
   */
  bindMultipartTask:function(){
	  var that = this;
	 wx.chooseImage({
		success:function(res){
			 fs.readFile({
          filePath: res.tempFilePaths[0],
          success: function (buffer) {
            var data = buffer.data;
            that.initMultipartTask(data);
          }
        });
		} 
	 });
  },
   initMultipartTask:function(data){
	  var that = this;
	  wx.request({
		  url:'http://172.17.150.42:8080/web/api/bos/ack.do',
		  dataType:'json',
		  method:'POST',
      data: {
        sts: '{}'
      },
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
		  success:function(res){
			   var key = "test/res/image/" + new Date().getTime() + '.jpg';
			   let ak = res.data.result.AccessKeyId,sk = res.data.result.SecretAccessKey,sessionToken = res.data.result.SessionToken
			   let client = new BosClient({
				 endpoint : 'http://bj.bcebos.com',
				 credentials : {
					 ak: ak ,
					 sk: sk
				 },
				 sessionToken : sessionToken,
				  createSignature : function (_, httpMethod, path, params, headers) {
					var credentials = {
					  ak: ak,
					  sk: sk
					};
					return Q.fcall(function () {
					  var auth = new Auth(credentials.ak, credentials.sk);
					  return auth.generateAuthorization(httpMethod, path, params, headers);
					});
				  }
			  });
			  var task = new MultipartTask(client,that,{
                bucket: 'zskd-pub',
                object: key,
          bos_multipart_auto_continue:true,
                  file : {
            blob : data,
            name : key
          },
          chunk_size : 4 * 1024 * 1024,
          bos_multipart_parallel : 1,
          bos_multipart_local_key_generator:'default'
        });
			  task.start(3);
		  }	
	  });
  }
  
});